﻿using System;
using System.IO;
using System.Net;

namespace MiniProxy
{
    public class Helper
    {
        public static void CopyStream(Stream output, Stream input)
        {
            //input.CopyTo(output);
            //input.Position = output.Position = 0;
            byte[] buffer = new byte[32768];
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, read);
            }
        }

        public static void CopyHeaders(HttpWebRequest to, HttpListenerRequest from)
        {
            foreach (string h in from.Headers.Keys)
            {
                Console.WriteLine("{0} = {1}", h, from.Headers[h]);
                switch (h)
                {
                    case "Accept":
                        to.Accept = from.Headers[h];
                        break;
                    case "Connection":
                        to.Connection = from.Headers[h];
                        break;
                    case "Content-Length":
                        to.ContentLength = Convert.ToInt64(from.Headers[h]);
                        break;
                    case "Content-Type":
                        to.ContentType = from.Headers[h];
                        break;
                    case "Date":
                        to.Date = DateTime.Parse(from.Headers[h]);
                        break;
                    case "Expect":
                        to.Expect = from.Headers[h];
                        break;
                    case "Host":
                        // avoid setting Host directly; derived from Url already
                        // request.Host = context.Request.Headers[h];
                        break;
                    case "If-Modified-Since":
                        to.IfModifiedSince = DateTime.Parse(from.Headers[h]);
                        break;
                    case "Range":
                        // TODO: to forward byte range
                        break;
                    case "Referer":
                        to.Referer = from.Headers[h];
                        break;
                    case "Transfer-Encoding":
                        to.TransferEncoding = from.Headers[h];
                        break;
                    case "User-Agent":
                        to.UserAgent = from.Headers[h];
                        break;
                    case "Proxy-Connection":
                        // TODO: to honor Proxy-Connection 
                        break;
                    default:
                        to.Headers.Add(h, from.Headers[h]);
                        break;
                }
            }
        }

        public static void CopyResponseHeaders(HttpListenerResponse to, WebResponse from)
        {
            foreach (string h in from.Headers.Keys)
            {
                Console.WriteLine("<={0} : {1}", h, from.Headers[h]);
                if (h == "Content-Length")
                {
                    to.ContentLength64 = Convert.ToInt64(from.Headers[h]);
                }
                else
                {
                    to.Headers[h] = from.Headers[h];
                }
            }
        }
    }
}
